package com.yunao.web.controller;

import com.yunao.commons.serachcore.core.bean.*;
import com.yunao.commons.utils.ResultVOUtil;
import com.yunao.commons.vo.ResultVO;
import com.yunao.service.report.KeywordUtils;
import com.yunao.web.annotation.UserLoginToken;
import com.yunao.web.convert.QueryForm2Map;
import com.yunao.web.form.QueryForm;
import com.yunao.service.IsearchService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/search/search")
public class SearchServerController {

	@Autowired
	IsearchService service;

	@UserLoginToken
	@PostMapping("/querySimple")
	public ResultVO query(QueryForm queryForm) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
		Map<String, String> params = QueryForm2Map.searchConvert(queryForm,null);
		Articles articles = service.query(params);
		return ResultVOUtil.success(articles);
	}
	@UserLoginToken
	@PostMapping("/query")
	public Articles query(@RequestBody Map<String, String> params) {
//		Map<String, String> params2 = RequestParamUtils.parseParams(request);
		Articles articles = service.query(params);
		return articles;
	}
	@UserLoginToken
	@PostMapping("/reprints")
	public Articles reprintQuery(
			@RequestBody Map<String, String> params) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
		String keywords = KeywordUtils.extract(params.get("text"));
		params.put("keywords", keywords);
		Articles articles = service.query(params);
		return articles;
	}

	/**
	 * showdoc
	 * @catalog API文档/搜索分析模块
	 * @title 文章详情
	 * @description 根据id展示文章详情
	 * @method get
	 * @url /search/search/{id}
	 * @return {"id": 677346407351209,"title": "","content": "吉非替尼片（伊瑞可）山东河南福建广东海南陕西山西安徽内蒙上海甘肃河北天津辽宁吉林黑龙江在哪里买? 吉非替尼片（伊瑞可）济南德州聊城青岛烟台日照威海东营淄博泰安临沂滨州枣庄潍坊在哪里卖? 通用名称：吉非替尼片 商品名称：吉非替尼片(伊瑞可) 【适应症/功能主治】 本品单药适用于表皮生长因子   展开全文c","summary": "吉非替尼片（伊瑞可）山东河南福建广东海南陕西山西安徽内蒙上海甘肃河北天津辽宁吉林黑龙江在哪里买。  吉非替尼片（伊瑞可）济南德州聊城青岛烟台日照威海东营淄博泰安临沂滨州枣庄潍坊在哪里卖。  通用名称：吉非替尼片 商品名称：吉非替尼片(伊瑞可) 【适应症/功能主治】 本品单药适用于表皮生长因子   展开全文c。","url": "http://weibo.com/1109051731/I6rocvDGh","datetime": 1568185429000,"author": "","images": "","videos": "","newsSource": "","newsClassify": "","domain": "weibo.com","domainSource": "新浪微博","mediaType": "","channel": "社会综合","emotion": "中性","authorType": "","messageType": "正文","location": "山东省","domainLevel": "境内其它","sensitiveWords": null,"highFrequencyWords": "吉非替尼片\t伊瑞可\t适应症\t哪里\t主治\t枣庄\t滨州\t表皮\t东营\t泰安\t内蒙\t聊城\t名称\t威海\t日照\t德州\t因子\t淄博\t潍坊\t临沂\t烟台","domestic": 1,"views": 0,"comments": 0,"likes": 0,"reprints": 0,"nr": "","ns": "","nt": "","subs": []}
	 * @remark 请求中的id是文章的id
	 * @number 01
	 *
	 */
	@UserLoginToken
	@GetMapping(path = "{id}")
	public Article queryById(@PathVariable long id) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
		Map<String, String> params = new HashMap<>();
		params.put("id", String.valueOf(id));
		Articles articles = service.query(params);
		if (articles.getSize() > 0)
			return articles.getData().get(0);
		throw new RuntimeException("文章id:"+id+"查询出错");
	}
	@UserLoginToken
	@PostMapping("/group")
	public Groups group(@RequestBody Map<String, String> params) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
//		params.put("group", group);
		Groups groups = service.group(params);
		return groups;
	}
	@UserLoginToken
	@PostMapping("/cube")
	public Cubes cube(@RequestBody Map<String, String> params) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
//		params.put("group", group);
//		params.put("group2", group2);
		Cubes cubes = service.cube(params);
		return cubes;
	}
	@UserLoginToken
	@PostMapping("/hotreprints")
	public Articles distinct(@RequestBody Map<String, String> params) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
//		params.put("keywords", keywords);
		if (!(params.containsKey("between") 
				|| params.containsKey("greaterequal") 
				|| params.containsKey("greaterthan"))) {
			params.put("greaterequal", String.valueOf(DateTime.now().millisOfDay().withMinimumValue().getMillis()));
		}
		Articles articles = service.distinct(params);
		return articles;
	}
	@UserLoginToken
	@PostMapping("/wordcloud")
	public Wordclouds wordcloud(@RequestBody Map<String, String> params) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
		Wordclouds wordclouds = service.wordcloud(params);
		return wordclouds;
	}
	@UserLoginToken
	@PostMapping("/wordrelation")
	public Wordrelations wordrelation(@RequestBody Map<String, String> params) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
		Wordrelations wordrelations = service.wordrelation(params);
		return wordrelations;
	}
	@UserLoginToken
	@PostMapping("/cluster")
	public Clusters cluster(@RequestBody Map<String, String> params) {
//		Map<String, String> params = RequestParamUtils.parseParams(request);
		Clusters clusters = service.cluster(params);
		return clusters;
	}


}
